package com.webserver.controller;

import com.webserver.core.annotations.Controller;
import com.webserver.core.annotations.RequestMapping;
import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.Article;
import com.webserver.vo.User;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 处理文章相关业务
 */
@Controller
public class ArticleController {
    //保存所有文章信息的目录的名字
    private static String articleDirName = "./articles/";

    static {
        //程序加载时判断一下保存所有用户信息的目录是否存在，不存在先自动创建出来
        File articleDir = new File(articleDirName);
        if(!articleDir.exists()){
            articleDir.mkdirs();
        }
    }


@RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpRequest request, HttpResponse response){
        System.out.println("处理发表文章...");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        if(title==null||author==null||content==null){
            response.setEntity(new File("./webapps/myweb/article_fail.html"));
            return;
        }
        try(
                ObjectOutputStream oos = new ObjectOutputStream(
                       new FileOutputStream(articleDirName+title+".obj")
                );
                ){
            Article article = new Article(title,content,author);
            oos.writeObject(article);
            //响应发表成功页面
            response.setEntity(new File("./webapps/myweb/article_success.html"));
        }catch(IOException e){
            e.printStackTrace();
        }


        System.out.println("发表文章处理完毕!");
    }

@RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpRequest request, HttpResponse response){
        System.out.println("开始生成所有文章动态页面...");
        List<Article> articlesList = new ArrayList<>();
        File articleDir = new File(articleDirName);
        File[] subs = articleDir.listFiles(f -> f.getName().endsWith(".obj"));
        for (File articleFile : subs) {
            try (
                    ObjectInputStream ois = new ObjectInputStream(
                            new FileInputStream(articleFile)
                    );
            ) {
                articlesList.add((Article) ois.readObject());
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        articlesList.forEach(System.out::println);


        PrintWriter pw = response.getWriter();
        pw.println("<!DOCTYPE html>");
        pw.println("<html lang=\"en\">");
        pw.println("<head>");
        pw.println("<meta charset=\"UTF-8\">");
        pw.println("<title>文章列表</title>");
        pw.println("</head>");
        pw.println("<body>");
        pw.println("<center>");
        pw.println("<table border=\"2\">");
        pw.println("<tr>");
        pw.println("<td align=\"center\">标题</td>");
        pw.println("<td align=\"center\">作者</td>");
        pw.println("<tr>");

        for (Article article:articlesList
             ) {
            pw.println("<tr>");
            pw.println("<td align=\"center\">"+article.getTitle()+"</td>");
            pw.println("<td align=\"center\">"+article.getAuthor()+"</td>");
            pw.println("<tr>");
        }

        pw.println("</center>");
        pw.println("</body>");
        pw.println("</html>");

        response.setContentType("text/html");


        System.out.println("文章动态页面生成完毕!");

    }

}
